﻿namespace S7.Net.Types
{
    using System;
    using System.Text;

    public class String
    {
        public static byte[] ToByteArray(string value, int reservedLength)
        {
            var length = value?.Length;
            if (length > reservedLength) length = reservedLength;
            var bytes = new byte[reservedLength];

            if (length == null || length == 0) return bytes;

            System.Text.Encoding.ASCII.GetBytes(value, 0, length.Value, bytes, 0);

            return bytes;
        }

        public static string FromByteArray(byte[] bytes)
        {
            return Encoding.ASCII.GetString(bytes);
        }

        public static byte[] ToByteArray(string value)
        {
            return Encoding.ASCII.GetBytes(value);
        }
    }
}

